﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using NCRGEA.AAL;

namespace NCRGEA.Members
{
    public partial class frmQuickSearch : Form
    {
        #region Global Variables

        /// <summary>
        /// This enum is used to determine which text box called the quick sort function
        /// </summary>
        enum code
        {
            FIRSTNAMETEXTBOX = 1,
            LASTNAMETEXTBOX,
            MIDDLENAMETEXTBOX
        }
        
        #endregion

        public frmQuickSearch()
        {
            InitializeComponent();
        }

        #region Control Manipulation
        
        /// <summary>
        /// Shows all the columns on the datagrid.
        /// </summary>
        private void enableColumns()
        {
            gcolFirstName.Visible = true;
            gcolID.Visible = true;
            gcolLastName.Visible = true;
            gcolMiddleInitial.Visible = true;
            gcolSocialSecurityNumber.Visible = true;
            gcolType.Visible = true;
        }

        /// <summary>
        /// Disables the columns on the datagrid
        /// </summary>
        private void disableColumns()
        {
            gcolFirstName.Visible = false;
            gcolID.Visible = false;
            gcolLastName.Visible = false;
            gcolMiddleInitial.Visible = false;
            gcolSocialSecurityNumber.Visible = false;
            gcolType.Visible = false;
        }

        #endregion

        #region Data Manipulation


        /// <summary>
        /// Searches the database whenever the user begins typing into the text boxes
        /// </summary>
        /// <param name="search"></param>
        private void searchDataBase(code nCode)
        {
            //The INT code is used to determine which text box called this function
            string query = @"SELECT TOP 50
                                mi.MemberID,
                                LEFT(mt.Name,1) TypeName,
                                mi.LastName,
	                            mi.FirstName,
                                mi.MiddleName,
	                            LEFT(SSN,3) + '-' + SUBSTRING(SSN,4,2) + '-' + RIGHT(SSN,4) FormatSSN
                            FROM
	                            memInfo mi
		                            INNER JOIN
			                            memTypes mt
		                            ON
			                            mi.TypeID = mt.TypeID
                             WHERE
                                mi.FirstName LIKE @fName + '%' AND
                                mi.LastName LIKE @lName + '%' AND
                                mi.MiddleName LIKE @mName + '%'";
            
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@fName", txtFirstName.Text) { SqlDbType = SqlDbType.VarChar };
            param[1] = new SqlParameter("@lName", txtLastName.Text) { SqlDbType = SqlDbType.VarChar };
            param[2] = new SqlParameter("@mName", txtMiddleName.Text) { SqlDbType = SqlDbType.VarChar };

            DataTable temp = DAL.ExecuteQuery(query, param);

            dcQuickSearch.DataSource = temp;
        }

        #endregion

        /// <summary>
        /// Whenever a user beings typing, determine if it is false. Otherwise, call search function
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtFirstName_TextChanged(object sender, EventArgs e)
        {
            if (!(txtFirstName.Text == string.Empty))
                searchDataBase(code.FIRSTNAMETEXTBOX);
            else
                dcQuickSearch.DataSource = null;
        }

        /// <summary>
        /// This searches for the Last Name
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtLastName_TextChanged(object sender, EventArgs e)
        {
            if (!(txtLastName.Text == string.Empty))
                searchDataBase(code.LASTNAMETEXTBOX);
            else
                dcQuickSearch.DataSource = null;
        }

        /// <summary>
        /// This searches for the middle name 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtMiddleName_TextChanged(object sender, EventArgs e)
        {
            if (!(txtMiddleName.Text == string.Empty))
                searchDataBase(code.MIDDLENAMETEXTBOX);
            else
                dcQuickSearch.DataSource = null;
        }

        private void dcQuickSearch_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.RowIndex > ((DataTable)(dcQuickSearch.DataSource)).Rows.Count)
                return;
            int MemberID = (int)((DataTable)(dcQuickSearch.DataSource)).Rows[e.RowIndex]["MemberID"];
            frmMain fMain = (frmMain)this.MdiParent;
            fMain.LaunchForm(new Members.frmViewMember(MemberID));
        }


    }
}
